package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.product.CategoryBrandDto;
import com.zhentao.entity.product.Brand;
import com.zhentao.entity.product.CategoryBrand;
import com.zhentao.service.CategoryBrandService;
import com.zhentao.vo.common.Result;
import com.zhentao.vo.common.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/product/categoryBrand")
public class CategoryBrandController {

    @Resource
    private CategoryBrandService categoryBrandService ;

    /**
     * 品牌分页管理
     * @param CategoryBrandDto
     * @return
     */
    @PostMapping("/")
    public Result<Page<CategoryBrand>> findByPage(@RequestBody CategoryBrandDto CategoryBrandDto) {
        Page<CategoryBrand> pageInfo = categoryBrandService.findByPage(CategoryBrandDto);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 添加分类信息
     * @param categoryBrand
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody CategoryBrand categoryBrand) {
        CategoryBrand categoryBrand1 = new CategoryBrand();
        categoryBrand1.setBrandId(categoryBrand.getBrandId());
        categoryBrand1.setCategoryId(categoryBrand.getCategoryId());
        categoryBrand1.setCreateTime(new Date());
        categoryBrand1.setIsDeleted(0);
        categoryBrandService.save(categoryBrand1);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 修改品牌分类信息
     * @param categoryBrand
     * @return
     */
    @PutMapping("updateById")
    public Result updateById(@RequestBody CategoryBrand categoryBrand) {
        categoryBrand.setUpdateTime(new Date());
        categoryBrandService.updateById(categoryBrand);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 删除品牌分类信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        categoryBrandService.removeById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    /**
     * 加载品牌数据
     * @param categoryId
     * @return
     */
    @GetMapping("/findBrandByCategoryId/{categoryId}")
    public Result findBrandByCategoryId(@PathVariable Long categoryId) {
        List<Brand> brandList =   categoryBrandService.findBrandByCategoryId(categoryId);
        return Result.build(brandList , ResultCodeEnum.SUCCESS) ;
    }
}